package com.person.ntv.controller.sys;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.Page;
import com.person.common.PageParams;
import com.person.exception.ServiceException;
import com.person.ntv.entity.sys.SysOrg;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysOrgService;

@Controller
@RequestMapping("/system/sysOrg")
public class SysOrgController {
	@Autowired
  private CommonService commonService;
	@Autowired
  private SysOrgService sysOrgService;
  
  @RequestMapping("/listPage")
	public String  getOrgListPage(Map<String,String> params,ModelMap mv){
	    initCode(mv);
		return "/system/org/orgList";
	}
	
	@RequestMapping("/orgList")
	@ResponseBody
	public Map<String,Object> getOrgList(PageParams myPage){
		Page<Map<String,Object>> dataList=sysOrgService.getOrgPage(myPage);
		for(int i=0;i<dataList.size();i++){
			Object pId=dataList.get(i).get("_parentId");
			boolean flag=true;
			for(int k=0;k<dataList.size();k++){
				 if(String.valueOf(dataList.get(k).get("id")).equals(String.valueOf(pId)))
					  flag=false;
			}
			if(flag)
				dataList.get(i).put("_parentId", null);
		}
      Map<String,Object> map=new HashMap<String,Object>();
      map.put("total",dataList.getTotal());
      map.put("rows",dataList.getResult());
	  return map;
	}
	
	@RequestMapping("/addPage")
	public String  addOrgPage(String pId,ModelMap mv){
		mv.addAttribute("pId", pId);
		 initCode(mv);
		return "/system/org/orgAdd";
	}
	@RequestMapping("/editPage")
	public String  editOrgPage(String id,ModelMap mv){
		try {
		initCode(mv);
	    SysOrg org = sysOrgService.selectByPrimaryKey(new BigDecimal(id));
	    mv.addAttribute("org",org);
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"查询出错！");
		}
		return "/system/org/orgEdit";
	}
	
	@RequestMapping("/addOrg")
	public String  addOrg(ModelMap mv,SysOrg sysOrg){
		try {
			if(sysOrg.getUpOrgId()==null){
				sysOrg.setUpOrgId(new BigDecimal(0));
				sysOrg.setUpOrgIds("0/");
			}else{
				sysOrg.setUpOrgIds(sysOrgService.selectByPrimaryKey(sysOrg.getUpOrgId()).getUpOrgIds()+sysOrg.getUpOrgId()+"/");
			}
			sysOrgService.insertSelective(sysOrg); 
			mv.addAttribute("error", "0");
			mv.addAttribute("msg","保存成功！");
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"保存失败！");
		}
		return "/system/org/orgAdd";
	}
	
	@RequestMapping("/editOrg")
	public String  editOrg(ModelMap mv,SysOrg sysOrg){
		try {
			sysOrgService.updateByPrimaryKeySelective(sysOrg); 
			mv.addAttribute("error", "0");
			mv.addAttribute("msg","保存成功！");
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"保存失败！");
		}
		return "/system/org/orgEdit";
	}
	
	@RequestMapping("/delOrg")
	@ResponseBody
	public Map<String,Object>  delOrg(BigDecimal[]ids,HttpServletRequest request){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			sysOrgService.deleteByPrimaryKeys(ids); 
			map.put("error", "0");
			map.put("msg","删除成功！");
		} catch (ServiceException e) {
			map.put("error", "1");
			map.put("msg",e.getMessage()+"删除失败！");
		}
		return map;
	}
	
	@RequestMapping("/detailPage")
	public String  detailOrgPage(String id,ModelMap mv){
		try {
	    SysOrg org = sysOrgService.selectByPrimaryKey(new BigDecimal(id));
	    mv.addAttribute("org",org);
		} catch (ServiceException e) {
			mv.addAttribute("error", "1");
			mv.addAttribute("msg",e.getMessage()+"查询出错！");
		}
		return "/system/org/orgDetail";
	}
  
	
	@RequestMapping("/validateOrgName")
	@ResponseBody
	public boolean validateOrgName(SysOrg  sysOrg) throws Exception{
		try {
			if(sysOrg.getOrgName()==null)sysOrg.setOrgName("");
			int cnt=commonService.validateOne("sys_org","org_name", sysOrg.getOrgName(),sysOrg.getId()==null?null:String.valueOf(sysOrg.getId()));
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
           e.printStackTrace();
		}
		return false;
	}
	
	@RequestMapping("/validateOrgAbbr")
	@ResponseBody
	public boolean validateOrgAbbr(SysOrg  sysOrg) throws Exception{
		try {
			if(sysOrg.getOrgAbbr()==null)sysOrg.setOrgAbbr("");
			int cnt=commonService.validateOne("sys_org","org_abbr",sysOrg.getOrgAbbr(),sysOrg.getId()==null?null:String.valueOf(sysOrg.getId())); 
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	@RequestMapping("/validateOrgCode")
	@ResponseBody
	public boolean validateOrgCode(SysOrg  sysOrg) throws Exception{
		try {
			if(sysOrg.getOrgCode()==null)sysOrg.setOrgCode("");
			int cnt=commonService.validateOne("sys_org","org_code",sysOrg.getOrgCode(),sysOrg.getId()==null?null:String.valueOf(sysOrg.getId())); 
			if(cnt==0)
			return true;
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return false;
	}
	private void initCode(ModelMap mv){
		mv.addAttribute("SysOrgStatus",JSONArray.fromObject(commonService.getCodeList("SysOrgStatus",true)).toString());
		mv.addAttribute("SysOrgType",JSONArray.fromObject(commonService.getCodeList("SysOrgType",true)).toString());
	}
}
